﻿-- Author  : Kurapica
-- Create Date : 2010/03/03

do	
	IGAS:NewAddon("IGAS_Studio")
	
	-- SystemEventReg Form
	local frm = CreateFrame("Form", "SystemEventReg", coreF)
	frm.Caption = L["Register SystemEvent"]	
	frm.FrameStrata = "FULLSCREEN_DIALOG"
	frm.Width = 540
	frm.Height = 400
	frm.Visible = false
	
	local eventList = CreateFrame("List", "SystemEventList", frm)
	eventList:SetPoint("TOPLEFT", frm, "TOPLEFT", 4, -24)
	eventList:SetPoint("RIGHT", frm, "CENTER")
	eventList:SetPoint("BOTTOMLEFT", frm, "BOTTOMLEFT", 4, 24)
	
	local btnOkay = CreateFrame("NormalButton", "Okay", frm)
	btnOkay:SetPoint("LEFT", frm, "CENTER")
	btnOkay:SetPoint("BOTTOMRIGHT", frm, "BOTTOMRIGHT", -4, 24)
	btnOkay.Height = 24
	btnOkay.Style = "NORMAL"
	btnOkay.Text = L["Save"]
	
	local btnAdd = CreateFrame("NormalButton", "Add", frm)
	btnAdd:SetPoint("BOTTOMLEFT", btnOkay, "TOPLEFT")
	btnAdd:SetPoint("BOTTOMRIGHT", btnOkay, "TOPRIGHT")
	btnAdd.Height = 24
	btnAdd.Style = "NORMAL"
	btnAdd.Text = L["Add Custom Event"]	
	
	local selectList = CreateFrame("List", "SelectedEventList", frm)
	selectList:SetPoint("TOPRIGHT", frm, "TOPRIGHT", -4, -24)
	selectList:SetPoint("LEFT", frm, "CENTER")
	selectList:SetPoint("BOTTOMRIGHT", btnAdd, "TOPRIGHT", 0, 4)

	-- CodeHelpDB
	local _DB = CreateHelpDB("SystemEvent")
	
	eventList:SuspendLayout()
	
	local _sort = {}
	for key in pairs(_DB) do
		tinsert(_sort, key)
	end
	sort(_sort)
	for _, key in pairs(_sort) do
		eventList:AddItem(_DB[key], key)
	end
	eventList:ResumeLayout()
	wipe(_sort)
	_sort = nil
	
	-- Scripts
	local _SelectObject, _IsAddon
	local substr = {}
	
	local function Decode(var)
		return (substr[var] and substr[var]) or ""
	end
	
	local function GetContent(script)
		return gsub(script, "@(%a+)", Decode)
	end
	
	local _SaveReg = [[
_Addon:RegisterEvent("@SystemEvent")
]]

	local function GetRegCode(eventList)
		local str = ""
		
		for event in pairs(eventList) do
			substr["SystemEvent"] = event
			str = str..GetContent(_SaveReg)
		end
		
		return str
	end
	
	function eventList:OnItemChoosed(key, item)
		frm.Message = (key and key.Intro) or ""
	end
	
	function eventList:OnItemDoubleClick(key, item)
		selectList:SetItem(item, item)
	end
	
	function selectList:OnItemDoubleClick(key, item)
		selectList:RemoveItem(key)
	end
	
	local function callback(text)
		if text then
			selectList:SetItem(text, text)
		end
	end
	
	function btnAdd:OnClick()
		IGAS:MsgBox("Please Input the custom event's name", "ic", callback)
	end
	
	function btnOkay:OnClick()
		if _SelectObject then
			_SelectObject.__RegisterEventList = _SelectObject.__RegisterEventList or {}
			
			wipe(_SelectObject.__RegisterEventList)
			
			for _, event in pairs(selectList.Items) do
				_SelectObject.__RegisterEventList[event] = true
			end
			
			if _IsAddon then
				local code = _SelectObject[2] and _SelectObject[2]["Content"]

				if code then
					if strfind(code, "_Addon:RegisterEvent%b()") then
						local index = strfind(code, "_Addon:RegisterEvent%b()") - 1
						
						code = gsub(code, "_Addon:RegisterEvent%b()\n", "")

						_SelectObject[2]["Content"] = strsub(code, 1, index)..GetRegCode(_SelectObject.__RegisterEventList)..strsub(code, index + 1)			
					else
						_SelectObject[2]["Content"] = code.."\n"..GetRegCode(_SelectObject.__RegisterEventList)
					end
					
					self:FireEvent("IGAS_STUDIO_FILERELOAD_REQUIRE", _SelectObject[2])
				end
			end
		end
		_SelectObject = nil
		_IsAddon = false
		selectList:Clear()
		frm.Visible = false
	end

	-- Events
	frm:RegisterEvent("IGAS_STUDIO_REGISTER_EVENT")
	
	function frm:IGAS_STUDIO_REGISTER_EVENT(object, isAddon)
		if not object or type(object) ~= "table" then
			return
		end
		_SelectObject = object
		_IsAddon = (isAddon and true) or false
		selectList:Clear()
		if object.__RegisterEventList and type(object.__RegisterEventList) == "table" then
			selectList:SuspendLayout()
			for event in pairs(object.__RegisterEventList) do
				selectList:AddItem(event, event)
			end
			selectList:ResumeLayout()
		end
		frm.Visible = true
	end
end